package com.itheima.controller;


import com.itheima.pojo.Result;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
/*
* LTAI5tKR7jH4nhgrkUphGMf1
U8bGAHLg06nQ0FASLUGiyMwXUzJ5IZ*/

@RestController
public class UpLoadController {

    private final com.itheima.utill.AliyunOSSOperator aliyunOSSOperator;

    public UpLoadController(com.itheima.utill.AliyunOSSOperator aliyunOSSOperator) {
        this.aliyunOSSOperator = aliyunOSSOperator;
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
       /* //获取文件原名
        String filename = file.getOriginalFilename();
        //获取扩展名
        String[] strings = filename.split("\\.");
        String kzm = strings[strings.length - 1];
        //把临时文件保存本地
        file.transferTo(new File("src/main/java/pricture" + UUID.randomUUID() + "." + kzm));*/
        //获取文件保存本地
        String originalFilename = file.getOriginalFilename();
        //上传文件对应的字节数组
        byte[] bytes = file.getBytes();
        //把文件上传到阿里云服务
        String url = aliyunOSSOperator.upload(bytes , originalFilename);
        return Result.success(url);
    }
}
